//
//  MBCompetationContentController.m
//  MiBaoTu
//
//  Created by admin on 16/11/11.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBCompetationContentController.h"
#import "MBRecordFreedomController.h"
#import "MBDetailViewController.h"

#import "MBCompetationContentCell.h"
#import "MBCompetationDetailContentCell.h"

#import "MBDetailViewForIpadController.h"
@interface MBCompetationContentController ()<UITableViewDataSource,UITableViewDelegate> {
    UITableView             *_tableView;
    UILabel                 *_showNoLb;
    NSMutableArray          *_mutArr;
    int                     page;
}

@end

@implementation MBCompetationContentController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self onLoadNew];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _mutArr = [[NSMutableArray alloc]init];
    page = 1;
    //this is show data for content
    [self onCreateUI];
    
}

- (void)onCreateUI
{
    CGFloat height = SCREEN_HEIGHT-NAV_HEIGHT-288*PERCEN-76*PERCEN-106*PERCEN;
    if (ISIPAD) {
        height=SCREEN_HEIGHT-NAV_HEIGHT-500*PERCEN-100*PERCEN-130*PERCEN;
    }
    if (_type == CompetationContentType_Peceo) {
        height = SCREEN_HEIGHT-NAV_HEIGHT-288*PERCEN-100*PERCEN;
        if (ISIPAD) {
            height = SCREEN_HEIGHT-NAV_HEIGHT-500*PERCEN-76*PERCEN;
        }
    }
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, height) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.backgroundView = nil;
    [self.view addSubview:_tableView];
     [_tableView addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(onLoadNew)];
     [_tableView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(onLoadMore)];
    
    UIView *bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, _tableView.bottom, SCREEN_WIDTH, 106*PERCEN)];
    if (ISIPAD) {
        bottomView.frame=CGRectMake(0, _tableView.bottom, SCREEN_WIDTH, 130*PERCEN);
        UIView*lineView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)];
        lineView.backgroundColor=UIColorFromHex(0xcccccc);
        [bottomView addSubview:lineView];
    }
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView];
    
    if (_type != CompetationContentType_Peceo) {
        UIButton *captaionBt = [UIButton buttonWithType:UIButtonTypeCustom];
        if (_isStart) {
            captaionBt.backgroundColor = UIColorFromHex(0xeb6fa7);
        } else {
            captaionBt.backgroundColor = UIColorFromHex(0x999999);
        }
        [captaionBt setTitle:@"我要参加" forState:0];
        [captaionBt setTitle:@"我要参加" forState:1];
        [captaionBt setTitleColor:[UIColor whiteColor] forState:0];
        [captaionBt setTitleColor:[UIColor whiteColor] forState:1];
        captaionBt.titleLabel.font = [UIFont systemFontOfSize:14.];
        captaionBt.frame = CGRectMake((bottomView.width-288*PERCEN)/2., (bottomView.height-68*PERCEN)/2., 288*PERCEN, 68*PERCEN);
        if (ISIPAD) {
             captaionBt.titleLabel.font = [UIFont systemFontOfSize:32*PERCEN];
             captaionBt.frame = CGRectMake((bottomView.width-800*PERCEN)/2., (bottomView.height-90*PERCEN)/2., 800*PERCEN, 90*PERCEN);
            
        }
        captaionBt.layer.cornerRadius = 5.;
        captaionBt.layer.masksToBounds = YES;
        [captaionBt addTarget:self action:@selector(onPushRecordAction:) forControlEvents:UIControlEventTouchUpInside];
        captaionBt.selected = _isStart;
        [bottomView addSubview:captaionBt];
    }
    
    _showNoLb = [[UILabel alloc]initWithFrame:CGRectMake(0, (_tableView.height-20)/2., _tableView.width, 20)];
    _showNoLb.text = @"结束后的活动才能获取获奖名单哦~";
    _showNoLb.textColor = UIColorFromHex(0x999999);
    _showNoLb.textAlignment = NSTextAlignmentCenter;
    _showNoLb.font = [UIFont systemFontOfSize:14.];
    _showNoLb.hidden = YES;
    _showNoLb.userInteractionEnabled = YES;
    [self.view addSubview:_showNoLb];
    [self.view insertSubview:_showNoLb aboveSubview:_tableView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onTapShowTitle)];
    [_showNoLb addGestureRecognizer:tap];
}

- (void)onTapShowTitle
{
    [self onLoadNew];
}

#pragma mark - actions
#pragma mark - 跳转 随便录制
- (void)onPushRecordAction:(UIButton *)bt
{
    if (bt.selected == NO) return;
    
    if (_dataDic) {        
        MBRecordFreedomController *recordFreemdomVC = [[MBRecordFreedomController alloc] init];
        recordFreemdomVC.activityId = [NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"ID"]];
        [self.navigationController pushViewController:recordFreemdomVC animated:YES];
    }
}

#pragma mark - 数据加载
- (void)onLoadNew
{
    page = 1;
    [self onLoadData];
}

- (void)onLoadMore
{
    ++page;
    [self onLoadData];
}

- (void)onLoadData
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSString *url ;
    if (_type == CompetationContentType_Hot) {
        url = [NSString stringWithFormat:FAMILYSHOW_ACTIVITYGEYINFO_URL_PATH,[_dataDic objectForKey:@"ID"],@1,[NSNumber numberWithInt:page]];
    }
    else if (_type == CompetationContentType_TuiJian) {
        url = [NSString stringWithFormat:FAMILYSHOW_ACTIVITYGEYINFO_URL_PATH,[_dataDic objectForKey:@"ID"],@2,[NSNumber numberWithInt:page]];
    }
    else if (_type == CompetationContentType_New) {
        url = [NSString stringWithFormat:FAMILYSHOW_ACTIVITYGEYINFO_URL_PATH,[_dataDic objectForKey:@"ID"],@3,[NSNumber numberWithInt:page]];
    }
    else if (_type == CompetationContentType_Peceo) {
        url = [NSString stringWithFormat:FAMILYSHOW_ACTIVITY_AWARDLIST_URL_PATH,[_dataDic objectForKey:@"ID"],[NSNumber numberWithInt:page]];
    }
    [[HTTPManager standerDefault]AFGetRequest:url view:nil success:^(id json) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [_tableView.header endRefreshing];
        [_tableView.footer endRefreshing];
        
        if ([json[@"ret_code"] integerValue]==0) {
            NSArray *arr = json[@"data"];
            if (arr.count > 0) {
                if (page == 1) {
                    [_mutArr removeAllObjects];
                }
                [_mutArr addObjectsFromArray:arr];
            }
        } else {
            [_mutArr removeAllObjects];
            [ErrorMessage showErrorMessage:json[@"message"] inView:self.view];
        }
        
        [_tableView reloadData];
        
        if (_type == CompetationContentType_Peceo) {
            //获奖
            if (_mutArr.count == 0) {
                _tableView.hidden = YES;
                _showNoLb.hidden = NO;
            } else {
                _tableView.hidden = NO;
                _showNoLb.hidden = YES;
            }
        } else {
            _tableView.hidden = NO;
            _showNoLb.hidden = YES;
        }
        
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [_tableView.header endRefreshing];
        [_tableView.footer endRefreshing];
    }];
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _mutArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_type != CompetationContentType_Peceo) {
        
        static NSString *cellID = @"Competation_activity_Cell";
        MBCompetationContentCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[MBCompetationContentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
        }
        
        if (_mutArr.count > indexPath.row) {
            NSDictionary *dic = [_mutArr objectAtIndex:indexPath.row];
            cell.dataDic = dic;
            cell.idString = [NSString stringWithFormat:@"%d",(int)indexPath.row];
        }
    
        
        return cell;
    }
    
    static NSString *cellID = @"Competation_peceo_Cell";
    MBCompetationDetailContentCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[MBCompetationDetailContentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    
    if (_mutArr.count > indexPath.row) {
        NSDictionary *dic = [_mutArr objectAtIndex:indexPath.row];
        cell.dataDic = dic;
        cell.idString = [NSString stringWithFormat:@"%d",(int)indexPath.row];
    }
    
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (ISIPAD) {
        return 180*PERCEN;
    }
    return 144*PERCEN;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UserManager *manager = [UserManager standerDefult];
    
    NSString *idStr ;
    if (manager.audioPlayerType != AudioPLayerType_Qinzix) {
        //初次进入
        idStr = @"";
        manager.audioPlayerType = AudioPLayerType_Qinzix;
    } else {
        //2次进入
        NSInteger selectIndex = manager.selectMusicIndex;
        NSArray *arr = manager.musicArr;
        Music *dics = [arr objectAtIndex:selectIndex];
        idStr = [NSString stringWithFormat:@"%@",dics.ID];
    }
    [manager setSelectMusicIndex:indexPath.row];
    [manager.musicArr removeAllObjects];
    [manager.musicArr addObjectsFromArray:[manager onTurnArrayFormQingZiXiuArray:_mutArr]];
    manager.selectMusicIndex = indexPath.row;
    [manager.qinzixiuArr removeAllObjects];
    [manager.qinzixiuArr addObjectsFromArray:_mutArr];
    if (ISIPAD) {
        MBDetailViewForIpadController*childShowVC=[[MBDetailViewForIpadController alloc]init];
        childShowVC.dataAllArr = _mutArr;
        childShowVC.selectMusicID = idStr;
        [self.navigationController pushViewController:childShowVC animated:YES];
        return;
    }
    MBDetailViewController *childShowVC = [[MBDetailViewController alloc] init];
    [childShowVC.dataAllArr addObjectsFromArray:_mutArr];
    childShowVC.selectMusicID = idStr;
    [self.navigationController pushViewController:childShowVC animated:YES];
}

@end
